<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>JavaScript原始类型和对象</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>JavaScript原始类型和对象</h1>    <p>
        under
            <a href="../../tags/javascript/">javascript</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-08-07</p>


    <p>在JavaScript里，以下是原始类型：</p>
<ul class="simple">
<li><tt class="docutils literal">undefined</tt>，表示未定义的值，如没有被赋值的变量、没有实参对应的形参、不存在的对象属性等</li>
<li><tt class="docutils literal">null</tt>，表示无效的或不存在的值（变量可能被隐式设为<tt class="docutils literal">undefined</tt>，但必须被显示设为<tt class="docutils literal">null</tt>）</li>
<li>数字类型<tt class="docutils literal">Number</tt>（如1、2.0），JavaScript里数字是64位的IEEE 754浮点数，没有整数类型</li>
<li>字符串类型<tt class="docutils literal">String</tt>（如&quot;a&quot;、&quot;Hello World&quot;），JavaScript里字符串其实是16位无符号整数序列（<a class="reference external" href="http://stackoverflow.com/questions/11141136/default-javascript-character-encoding">不是标准的UTF-16编码</a>，比如UTF-16里U+10000以上的字符<a class="reference external" href="https://en.wikipedia.org/wiki/UTF-16">需要用两个16位无符号整数来编码</a>，但<tt class="docutils literal">String.prototype</tt>里只有很少的方法会按1个字符处理，而且这些方法好像是ECMAScript 2015才引进的）。</li>
<li>布尔类型<tt class="docutils literal">Boolean</tt>，<tt class="docutils literal">true</tt>或<tt class="docutils literal">false</tt>，表示逻辑值</li>
<li><tt class="docutils literal">Symbol</tt>（ECMAScript 2015引入）</li>
</ul>
<p>其余类型都是对象，对象的本质是键值对，类似其它语言的散列表或词典类型，包括函数、正则表达式等，原始类型数字、字符串和布尔的包装函数<tt class="docutils literal">Number</tt>、<tt class="docutils literal">String</tt>和<tt class="docutils literal">Boolean</tt>也可以用来创建对象。</p>
<div class="section" id="id3">
<h2>原始类型和对象的区别</h2>
<ul class="simple">
<li>如果v是对象，则<tt class="docutils literal">v instanceof Object</tt>为真，否则为假</li>
<li>typeof可以给出一些更具体的类型信息，但因为历史原因<tt class="docutils literal">typeof null === &quot;object&quot;</tt></li>
<li>如果v是对象，则<tt class="docutils literal">v.PROPERTY = VALUE</tt>有效，否则无效，<tt class="docutils literal">null</tt>和<tt class="docutils literal">undefined</tt>还会直接甩出<tt class="docutils literal">TypeError</tt></li>
</ul>
<p>可运行以下代码来验证：</p>
<div class="highlight"><pre><span></span><span class="kd">function</span> <span class="nx">testVal</span><span class="p">(</span><span class="nx">expr</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">try</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="nb">Function</span><span class="p">(</span><span class="s1">&#39;return &#39;</span> <span class="o">+</span> <span class="nx">expr</span><span class="p">)</span> <span class="p">();</span>
  <span class="p">}</span>
  <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;// Invalid expression: &#39;</span> <span class="o">+</span> <span class="nx">expr</span><span class="p">);</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;// &#39;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;\n&#39;</span><span class="p">);</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;var v = &#39;</span> <span class="o">+</span> <span class="nx">expr</span> <span class="o">+</span> <span class="s1">&#39;;&#39;</span><span class="p">);</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;v instanceof Object; // &#39;</span> <span class="o">+</span> <span class="p">(</span><span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">));</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;typeof v; // &#39;</span> <span class="o">+</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">v</span><span class="p">));</span>
  <span class="k">try</span> <span class="p">{</span>
    <span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;v.x = 1;\nv.x; // &#39;</span> <span class="o">+</span> <span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">e</span> <span class="k">instanceof</span> <span class="nx">TypeError</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;// v.x = 1; // &#39;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
      <span class="k">throw</span> <span class="nx">e</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span>
<span class="p">}</span>


<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;null&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;undefined&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;1&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;true&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;&quot;abc&quot;&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;Symbol(&quot;x&quot;)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;Number(2)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;Boolean(0)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;String(2006)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;new Number(1)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;new Boolean(1)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;new String(8.5)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;new Symbol(&quot;y&quot;)&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;[]&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;/abc/&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;function () {}&#39;</span><span class="p">);</span>
<span class="nx">testVal</span><span class="p">(</span><span class="s1">&#39;new Date()&#39;</span><span class="p">);</span>
</pre></div>
<p>输出如下：</p>
<div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="c1">// v.x = 1; // TypeError: Cannot set property &#39;x&#39; of null</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="kc">undefined</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// undefined</span>
<span class="c1">// v.x = 1; // TypeError: Cannot set property &#39;x&#39; of undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// number</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// boolean</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="s2">&quot;abc&quot;</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// string</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="nx">Symbol</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// symbol</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="nb">Number</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// number</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="nb">Boolean</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// boolean</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="nb">String</span><span class="p">(</span><span class="mi">2006</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// false</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// string</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// undefined</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Number</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Boolean</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">String</span><span class="p">(</span><span class="mf">8.5</span><span class="p">);</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="c1">// Invalid expression: new Symbol(&quot;y&quot;)</span>
<span class="c1">// TypeError: Symbol is not a constructor</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="p">[];</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="sr">/abc/</span><span class="p">;</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{};</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// function</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>

<span class="kd">var</span> <span class="nx">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">();</span>
<span class="nx">v</span> <span class="k">instanceof</span> <span class="nb">Object</span><span class="p">;</span> <span class="c1">// true</span>
<span class="k">typeof</span> <span class="nx">v</span><span class="p">;</span> <span class="c1">// object</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">v</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span> <span class="c1">// 1</span>
</pre></div>
<p>（完）</p>
</div>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>